package com.ansys.demo.service.impl;

import com.ansys.demo.bean.Problem;
import com.ansys.demo.mapper.QuestionMapper;
import com.ansys.demo.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: panghai
 * @Date: 2022/04/05/21:59
 * @Description:
 */
@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 题目总数（默认7题）
     */
    @Value("${questionCount}")
    private int questionCount = 7;

    /**
     * 添加题目
     * @return 题目对象
     */
    @Override
    public int insertQuestion(Problem problem) {
        return questionMapper.insertQuestion(problem);
    }

    /**
     * 检查题目是否已存在
     *
     * @param problem 题目
     * @return 1表示已存在，0表示不存在
     */
    @Override
    public int checkQuestion(Problem problem) {
        return questionMapper.checkQuestion(problem) >= 1 ? 1 : 0;
    }

    /**
     * 获取题目数目
     *
     * @return 题目数目
     */
    @Override
    public int getQuestionsCount() {
        return questionMapper.getQuestionsCount();
    }

    /**
     * 获取随机题目
     * @return 题目列表
     */
    @Override
    public List<Problem> getRandomQuestions() {
        return questionMapper.getRandomQuestions().subList(0, questionCount);
    }

}
